from flask import Flask, request, render_template_string

app = Flask(__name__)

# 页面模板
html_template = '''
<!DOCTYPE html>
<html>
<head>
    <title>基础代谢计算器</title>
</head>
<body>
    <h1>基础代谢计算器</h1>
    <form method="post">
        <label for="gender">性别：</label>
        <select id="gender" name="gender" required>
            <option value="male">男</option>
            <option value="female">女</option>
        </select><br><br>
        
        <label for="age">年龄（岁）：</label>
        <input type="number" id="age" name="age" min="0" required><br><br>
        
        <label for="height">身高（cm）：</label>
        <input type="number" step="0.1" id="height" name="height" min="0" required><br><br>
        
        <label for="weight">体重（kg）：</label>
        <input type="number" step="0.1" id="weight" name="weight" min="0" required><br><br>
        
        <button type="submit">计算</button>
    </form>
    
    {% if result %}
        <h2>基础代谢率：{{ result }} 千卡/天</h2>
    {% endif %}
</body>
</html>
'''

@app.route('/', methods=['GET', 'POST'])
def calculator():
    result = None
    if request.method == 'POST':
        gender = request.form['gender']
        age = float(request.form['age'])
        height = float(request.form['height'])
        weight = float(request.form['weight'])
        
        # 计算基础代谢率（Mifflin-St Jeor 公式）
        if gender == 'male':
            bmr = (10 * weight) + (6.25 * height) - (5 * age) + 5
        else:
            bmr = (10 * weight) + (6.25 * height) - (5 * age) - 161
        
        result = round(bmr, 2)
    
    return render_template_string(html_template, result=result)

if __name__ == '__main__':
    app.run(debug=True) 